
import File from '../file/File';
import { appendExt, joinPath } from '../Utils';
import Loader from './Loader';



export default class SourceLoader extends Loader {
    prepare(file: File) {
        file.id = appendExt(file.id, '.js');
    }

    async load(file: File) {
        let codeOpt: any = {
            id: file.id,
            dependency: {}
        };
        if (file.extraSourceOptions) {
            Object.assign(codeOpt, file.extraSourceOptions);
        }
        for (let i in file.dependencies) {
            codeOpt.dependency[i] = file.dependencies[i].id;
        }
        file.contents = this.wrapSource(codeOpt, file.contents);
    }

    wrapSource(opt: object, code?: string) {
        return `zpack.define(${JSON.stringify(opt, null, '\t')}, function(require, exports, module, __dirname, __filename) {\n\n ${code || ''} \n\n})`;
    }
}